package com.mindflow.hbase.tutorials.crud;

import com.mindflow.hbase.tutorials.zookeeper.ActiveKeyValueStore;
import org.apache.zookeeper.KeeperException;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class ConfigUpdater {
    public static final String PATH = "/config";
    private ActiveKeyValueStore store;
    private Random random = new Random();

    private boolean sw = false;

    public void setSw(boolean sw) {
        this.sw = sw;
    }

    public ConfigUpdater(String hosts) throws IOException, InterruptedException {
        store = new ActiveKeyValueStore();
        store.connect(hosts);
    }

    public void run() throws InterruptedException, KeeperException {
        while (sw) {
            String value = random.nextInt(100) + "";
            store.write(PATH, value);
            System.out.printf("Set	%s	to	%s\n", PATH, value);
            TimeUnit.SECONDS.sleep(random.nextInt(10));
        }


    }

    public static void main(String[] args) throws Exception {
        ConfigUpdater configUpdater = new ConfigUpdater(args[0]);
        configUpdater.run();
    }
}
